package entity.general;

import java.io.Serializable;

public class SecTime implements Serializable, Comparable <SecTime>, Time {
	int hours = 0, minutes = 0, seconds = 0;
	
	public SecTime(int h, int m, int s)
	{
		if(seconds >= 60)
			throw new RuntimeException("Seconds " + seconds + " > 60");
		if(minutes >= 60)
			throw new RuntimeException("Minutes " + minutes + " > 60");
//		if(hours > 24)
//			throw new RuntimeException("Hours " + hours + " > 24");
//		if(hours == 24 && minutes != 0)
//			throw new RuntimeException("Hours is 24, minutes is " + minutes + " > 0");
			
		hours = h;
		minutes = m;
	}
	
	public SecTime(int _seconds) {
		hours = _seconds / 60 / 60;
		minutes = _seconds / 60 % 60;
		seconds = _seconds % 60;
	}

	public int getSeconds()
	{
		return seconds;
	}
	
	public int getMinutes()
	{
		return minutes;
	}
	
	public int getHours()
	{
		return hours;
	}

	@Override
	public int compareTo(SecTime arg0) {
		if(hours > arg0.getHours())
			return 1;
		else if(hours < arg0.getHours())
			return -1;
		else if(minutes > arg0.getMinutes())
		{
			return 1;
		}
		else if(minutes < arg0.getMinutes())
		{
			return -1;
		}
		else if(seconds > arg0.getSeconds())
		{
			return 1;
		}
		else if(seconds < arg0.getSeconds())
		{
			return -1;
		}		
		return 0;
	}

	public SecTime add(SecTime time) {
		int nseconds = time.seconds + seconds;
		int nminutes = time.minutes + minutes;
		int nhours = time.hours + hours;
		
		return new SecTime(nseconds + nminutes * 60 + nhours * 60 * 60);
	}	

	public int hashCode()
	{
		return seconds + minutes * 60 + hours * 60 * 60;
	}
	
	public boolean equals(Object o)
	{
		if(o instanceof SecTime)
		{
			SecTime ot = (SecTime)o;
			if(ot.getHours() == hours && ot.getMinutes() == minutes && ot.getSeconds() == seconds)
			{
				return true;
			}
		}
		return false;
	}
	
	public String toString()
	{
		String str = "";
		if(hours == 0)
		{
			str += "00:";
		}
		else if(hours < 10)
		{
			str += "0" + hours +  ":";			
		}
		else
		{
			str += hours +  ":";						
		}
		
		if(minutes == 0)
		{
			str += "00:";
		}
		else if(minutes < 10)
		{
			str += "0" + minutes + ":";			
		}
		else
		{
			str += minutes + ":";						
		}

		if(seconds == 0)
		{
			str += "00";
		}
		else if(seconds < 10)
		{
			str += "0" + seconds;			
		}
		else
		{
			str += seconds;						
		}

		return str;
	}

	public int getAsSeconds() {
		return seconds + minutes * 60 + hours * 60 * 60;
	}

	public MinTime toMinTime() {
		int minutes = this.minutes + (seconds + 30) / 60;
		int hours = this.hours;
		if(minutes >= 60)
		{
			hours++;
			minutes = 0;
		}
		return new MinTime(hours, minutes);
	}
}
